<?php
$lines = file('termsEn.csv');

$apiKey = 'B8196E44B7B28856D42FFB920922145A367B2FD0';
$url = "http://api.microsofttranslator.com/V2/Http.svc/Translate?appId=$apiKey&from=en&to=nl&text=";

//$text = "This is a sample text";
//$test = $url . urlencode($text);
//$result = file_get_contents($test);
//echo strip_tags($result);

$upadtes = array();

foreach ($lines as $l=>$line) {
    $parts = split('","', $line);
    foreach ($parts as $p=>$part) {
        $parts[$p] = trim(str_replace('"',"", $part));
    }
    
    if(is_numeric($parts[1])){
        $strEn = $parts[3];
        preg_match_all('/\[[^\]]+\]/is', $strEn, $matches);
        
        $replaceParts = $matches[0];
        
        foreach ($replaceParts as $pos=>$term) {
            $strEn = str_replace($term, '|'.$pos.'|', $strEn);
        }
        
        
        $transUrl = $url . urlencode($parts[3]);
    //    print $transUrl;
        $result = file_get_contents($transUrl);
        $strTrans = strip_tags($result);
        
        foreach ($replaceParts as $pos=>$term) {
            $strTrans = str_replace('|'.$pos.'|', $term , $strTrans);
        }
        
        $parts[0] = $strTrans;
    //    $lines[$l] = $parts;
    
        
            $updates[] = "UPDATE Glossary SET meaning_nl='".mysql_escape_string($parts[0])."' WHERE id={$parts[1]};";
    
//    echo $l;
        if($l>10){
            //break;
        }
    }
}

//print_r($lines);
print_r(join("\n",$updates));